package com.example.myapplication.activity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.myapplication.R;
import com.example.myapplication.fragment.AddressBookFragment;
import com.example.myapplication.fragment.DiscoveryFragment;
import com.example.myapplication.fragment.MessageFragment;
import com.example.myapplication.fragment.SelfFragment;
import com.example.myapplication.utils.PremissionUtil;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    private LinearLayout messageLayout;
    private LinearLayout addressBookLayout;
    private LinearLayout discoveryLayout;
    private LinearLayout selfLayout;
    private ImageView messageImageView;
    private ImageView addressBookImageView;
    private ImageView discoveryImageView;
    private ImageView selfImageView;
    private MessageFragment messageFragment;
    private FragmentManager fragmentManager;
    private AddressBookFragment addressBookFragment;
    private DiscoveryFragment discoveryFragment;
    private SelfFragment selfFragment;
    private int currentFragmentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getSupportFragmentManager();
        bindView();
        defaultSetting();
        PremissionUtil.checkPermissionREAD_EXTERNAL_STORAGE(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = new MenuInflater(this);
        inflater.inflate(R.menu.menu_tools, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.new_chat:
                break;
            case R.id.add_friend:
                break;
            case R.id.qrcode_scan:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    void defaultSetting() {
        setTitle("消息");
        messageImageView.setImageDrawable(getDrawable(R.drawable.icon_message_fill));
        currentFragmentIndex = 1;
        messageFragment = new MessageFragment();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fl_main_window, messageFragment, "MessageFragment");
        fragmentTransaction.commit();
    }

    /**
     * 设置当前选中的Icon高亮
     * @param fragmentIndex
     */
    void setHightLighting(int fragmentIndex) {
        switch (fragmentIndex) {
            case 1:
                messageImageView.setImageDrawable(getDrawable(R.drawable.icon_message_fill));
                break;
            case 2:
                addressBookImageView.setImageDrawable(getDrawable(R.drawable.icon_addressbook_fill));
                break;
            case 3:
                discoveryImageView.setImageDrawable(getDrawable(R.drawable.icon_discovery_fill));
                break;
            case 4:
                selfImageView.setImageDrawable(getDrawable(R.drawable.icon_self_fill));
                break;
        }
    }

    /**
     * 清除指定Icon的高亮
     * @param fragmentIndex
     */
    void clearHighLighting(int fragmentIndex) {
        switch (fragmentIndex) {
            case 1:
                messageImageView.setImageDrawable(getDrawable(R.drawable.icon_message));
                break;
            case 2:
                addressBookImageView.setImageDrawable(getDrawable(R.drawable.icon_addressbook));
                break;
            case 3:
                discoveryImageView.setImageDrawable(getDrawable(R.drawable.icon_discovery));
                break;
            case 4:
                selfImageView.setImageDrawable(getDrawable(R.drawable.icon_self));
                break;
        }
    }

    /**
     * 控件绑定
     */
    void bindView() {
        messageLayout = findViewById(R.id.rl_message);
        addressBookLayout = findViewById(R.id.rl_addressbook);
        discoveryLayout = findViewById(R.id.rl_discovery);
        selfLayout = findViewById(R.id.rl_self);
        messageImageView = findViewById(R.id.iv_message);
        addressBookImageView = findViewById(R.id.iv_addressbook);
        discoveryImageView = findViewById(R.id.iv_discovery);
        selfImageView = findViewById(R.id.iv_self);

        messageLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // MessageFragment
                messageFragment = new MessageFragment();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl_main_window, messageFragment, "MessageFragment");
                fragmentTransaction.commit();

                setTitle("消息");
                clearHighLighting(currentFragmentIndex);
                currentFragmentIndex = 1;
                setHightLighting(currentFragmentIndex);
            }
        });
        addressBookLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // AddressBookFragment
                addressBookFragment = new AddressBookFragment();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl_main_window, addressBookFragment, "AddressBookFragment");
                fragmentTransaction.commit();

                setTitle("通讯录");
                clearHighLighting(currentFragmentIndex);
                currentFragmentIndex = 2;
                setHightLighting(currentFragmentIndex);
            }
        });
        discoveryLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // DiscoveryFragment
                discoveryFragment = new DiscoveryFragment();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl_main_window, discoveryFragment, "DiscoveryFragment");
                fragmentTransaction.commit();

                setTitle("发现");
                clearHighLighting(currentFragmentIndex);
                currentFragmentIndex = 3;
                setHightLighting(currentFragmentIndex);
            }
        });
        selfLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // SelfFragment
                selfFragment = new SelfFragment();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl_main_window, selfFragment, "SelfFragment");
                fragmentTransaction.commit();

                setTitle("我");
                clearHighLighting(currentFragmentIndex);
                currentFragmentIndex = 4;
                setHightLighting(currentFragmentIndex);

            }
        });

    }

}
